package com.shuqi.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.utils.Log4an;
import com.shuqi.controller.R;
import com.shuqi.controller.R.color;
import com.shuqi.controller.R.drawable;

/***
 * 收费版阅读页主题样式 提供者 夜间：6 / 默认：0 /暗黄：1 /护眼绿：2 /深绿：3 /浅灰：4 /深蓝：5 夜间：4 / 默认：0 /暗黄：1 /护眼绿：2 /浅灰：3
 * 
 * @author vip
 */
public class PayTheme {
    /** 夜间模式 */
    public static final int PAY_THEME = 8;// 夜间模式在主题数组中的最后位置(但不算主题模式)
    public static final int DEFAULT_THEME = 1;

    public static int getCurrentTheme(Context context) {
         SharedPreferences sp = context.getSharedPreferences("booksettings",
         Context.MODE_PRIVATE);
         return sp.getInt("ptheme", 1);
//        return SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentTheme();
    }

    public static int getCurrentTheme() {
         SharedPreferences sp = ShuqiApplication.getContext().getSharedPreferences("booksettings",
         Context.MODE_PRIVATE);
         return sp.getInt("ptheme", 1);
//        return SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentTheme();
    }

    public static boolean isNightMode(Context context) {
         SharedPreferences sp = context.getSharedPreferences("booksettings",
         Context.MODE_PRIVATE);
         return sp.getBoolean("isnightmode", false);
//        return SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentNightMode();
    }

    /**
     * 根据名称获取当前主题对应的的颜色
     * 
     * @param context
     * @param name payTheme1_<b>name<b>
     * @return
     */
    @Deprecated
    public int findColorByName(Context context, String name) {
        int payTheme = getCurrentTheme(context);
        if (isNightMode(context)) {
            payTheme = PAY_THEME;
//            Log4an.e("yjd", "findColorByName() " + payTheme + "_" + name);
        } else {
//            Log4an.e("yjd", "findColorByName() " + payTheme + "_" + name);
        }
        try {
            Class<color> clazz = R.color.class;
            int resId = clazz.getField("ptheme" + payTheme + "_" + name).getInt(clazz);
            return context.getResources().getColor(resId);
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 根据名称获取当前主题对应的的图片ID
     * 
     * @param context
     * @param name payTheme1_<b>name</b> <br>
     *        background 阅读页背景图 <br>
     *        icon_lock 目录锁图标 <br>
     *        icon_right 目录开放图标
     * 
     * @return 返回图片资源ID
     */
    public static int findDrawableResIDByName(Context context, String name) {
        return findDrawableResIDByName(context, name, false);
    }

    /**
     * 根据名称获取当前主题对应的的图片ID
     * 
     * @param context
     * @param name payTheme1_<b>name</b> <br>
     *        background 阅读页背景图 <br>
     *        icon_lock 目录锁图标 <br>
     *        icon_right 目录开放图标
     * @param is2Mode 是否只区分夜间模式和非夜间模式
     * @return 返回图片资源ID
     */
    public static int findDrawableResIDByName(Context context, String name, boolean is2Mode) {
        int payTheme = 0;
        if (!is2Mode) {
            payTheme = getCurrentTheme(context);
        }
        // if (isNightMode(context)) {
        // payTheme = 4;
        // }
        Log4an.e("yjd", "findDrawableRes is2Mode：" + is2Mode + " :" + payTheme + "_" + name);
        try {
            Class<drawable> clazz = R.drawable.class;
            Log4an.e("yjd", "The theme name is：" + "ptheme" + payTheme + "_" + name);
            return clazz.getField("ptheme" + payTheme + "_" + name).getInt(clazz);
        } catch (Exception e) {
            return 0;
        }
    }


}
